package cn.kgc.listener.sms;

import cn.hutool.json.JSONUtil;
import cn.kgc.listener.AbstractMessageListener;
import cn.kgc.message.SmsMessage;
import cn.kgc.utils.SmsSenderRetry;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Administrator
 */
@Component
@Slf4j
public class SmsMessageListener extends AbstractMessageListener {

    @Resource
    private SmsSenderRetry smsSenderRetry;

    @Override
    public void receiveMessage(Message message, Channel channel) throws Exception {
        log.warn("收到消息:{}", new String(message.getBody()));
        String jsonString=new String(message.getBody());
        SmsMessage smsMessage = JSONUtil.toBean(jsonString, SmsMessage.class);
        smsSenderRetry.sendSms(
                smsMessage.getPhone(),
                smsMessage.getCode()
        );
    }
}
